//
//  HomeTableViewController.swift
//  PersonnalDiary
//
//  Created by 徐宽阔 on 15/9/27.
//  Copyright © 2015年 XDKOO. All rights reserved.
//

import UIKit

class HomeTableViewController: BaseTableViewController {
    
    override func viewDidLoad() {
       super.viewDidLoad()
        if !isLogin
        {
            visitorView?.setupVisitorInfo(true, imageName: "visitordiscover_feed_image_house", info: "关注一些人，回这里看看有什么惊喜")
            return // 一定要return
        }
        setUpNav()
    }
    
    private func setUpNav()
    {
        // 便利构造器使用
        navigationItem.leftBarButtonItem = UIBarButtonItem.init(imageName: "navigationbar_friendattention", target: self, actionName: "friendattention")
        navigationItem.rightBarButtonItem = UIBarButtonItem.init(imageName: "navigationbar_pop", target: self, actionName: "pop")
        
        
        titleBtn.setTitle("xdkoo" + "  ", forState: UIControlState.Normal)
        titleBtn.setImage(UIImage(named: "navigationbar_arrow_down"), forState: UIControlState.Normal)
        titleBtn.setImage(UIImage(named: "navigationbar_arrow_up"), forState: UIControlState.Selected)
        titleBtn.addTarget(self, action: Selector("titleClick"), forControlEvents: UIControlEvents.TouchUpInside)
        navigationItem.titleView = titleBtn
    }
    
    
    @objc private func friendattention()
    {
        print(__FUNCTION__)
    }
    
    @objc private func pop()
    {
        let QRCode = UIStoryboard(name: "QRCodeViewController", bundle: nil).instantiateInitialViewController()!
        presentViewController(QRCode, animated: true, completion: nil)
    }
    
    
    @objc private func titleClick()
    {
        let popver = UIStoryboard(name: "PopverController", bundle: nil).instantiateInitialViewController()!
        
        popver.modalPresentationStyle = UIModalPresentationStyle.Custom
        popver.transitioningDelegate = animator
        
        presentViewController(popver, animated: true, completion: nil)
    }
    
    func changeTitleBtnStatus()
    {
         titleBtn.selected = !titleBtn.selected
    }
    
    private lazy var titleBtn:TitleButton = {
       let btn = TitleButton()
        return btn
    }()
    
    
    private lazy var animator:PopverAnimator = {
       let animator = PopverAnimator()
        animator.PopverFrame = CGRect(x: 50, y: 56, width: 300, height: 300)
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("changeTitleBtnStatus"), name: XDKPopverAnimatorDidShow, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("changeTitleBtnStatus"), name: XDKPopverAnimatorDidDisMiss, object: nil)
        
        return animator
    }()
    
    deinit
    {
       NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
}


